// pages/category/index.js
const service = require("../../service/port.js");
//import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navLeftItems: [],
    navRightItems: [],
    curIndex: 0,
    currentcid1:'',
    colorList:[],
    categoryTit:""
  },
  switchRightTab(e) {
    // Toast.loading({
    //   mask: true,
    //   message: '加载中...',
    // });
    let index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index,
      currentcid1:e.currentTarget.dataset.item.id,
      categoryTit:e.currentTarget.dataset.item.name
    })
    var that = this
    var colorList = that.data.colorList
    for (var i in colorList) {
      colorList[i] = ""
    }
    colorList[e.currentTarget.dataset.index] = "color:#e93b3d"
    that.setData({
      colorList
    })
    this.getcategoryList2(e.currentTarget.dataset.item.id)
  },
  onChange(event) {
    console.log(event)
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcategoryList1()
  },
  getcategoryList1:function(){
    service.getcategoryList1(res=>{
      //console.log(res)
      this.setData({
        navLeftItems:res.data
      })
      if(res.data[0]){
        // Toast.loading({
        //   mask: true,
        //   message: '加载中...',
        // });
        this.setData({
          currentcid1:res.data[0].id,
          categoryTit:res.data[0].name
        })        
        this.getcategoryList2(res.data[0].id)
      }
    })
    
    
  },
  getcategoryList2:function(id){
    wx.showLoading({
      title: '加载中',
    })
    service.getcategoryList2(id,res=>{
      //console.log(res)
      wx.hideLoading()
      this.setData({
        navRightItems:res.data
      })
    })
  },
  showListView(e) {
    let cid2 = e.currentTarget.dataset.cid2
    let title = e.currentTarget.dataset.tit
    wx.navigateTo({
      url: '/pages/categorylist/index?title=' + title+'&&cid2='+cid2+"&&cid1="+this.data.currentcid1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})